package com.google.code.jdbclogger;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.google.code.jdbclogger.logging.Logger;

/**
 * @author Andrey Vityuk
 */
public class LoggableDataSource implements DataSource {
    private DataSource dataSource;
    private Logger logger;

    public LoggableDataSource() {
    }

    public LoggableDataSource(DataSource dataSource, Logger logger) {
	this.dataSource = dataSource;
	this.logger = logger;
    }

    public void setDataSource(DataSource dataSource) {
	this.dataSource = dataSource;
    }

    public void setLogger(Logger logger) {
	this.logger = logger;
    }

    public Connection getConnection() throws SQLException {
	return ConnectionFactory.create(dataSource.getConnection(), logger);
    }

    public Connection getConnection(String username, String password) throws SQLException {
	return ConnectionFactory.create(dataSource.getConnection(username, password), logger);
    }

    public int getLoginTimeout() throws SQLException {
	return dataSource.getLoginTimeout();
    }

    public void setLoginTimeout(int seconds) throws SQLException {
	dataSource.setLoginTimeout(seconds);
    }

    public PrintWriter getLogWriter() throws SQLException {
	return dataSource.getLogWriter();
    }

    public void setLogWriter(PrintWriter out) throws SQLException {
	dataSource.setLogWriter(out);
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
	return dataSource.isWrapperFor(iface);
    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
	return dataSource.unwrap(iface);
    }
}
